<html><head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <link href="http://www.mcs.anl.gov/petsc/style.css" rel="stylesheet" type="text/css">
    <title>PETSc Hands On</title>
    <style type="text/css"></style>
</head>

    <div id="logo">
      <h1>PETSc</h1>
    </div>

    <div id="header">
      <h1>Documentation: Tutorials: Hands On Exercises</h1>
    </div>

    <hr>

    <div id="sidebar">
      <ul>
        <li><a href="../../index.html">Home</a></li>
        <li><a href="../../download/index.html">Download</a></li>
        <li><a href="../../features/index.html">Features</a></li>
        <li>
          <a href="../index.html">Documentation</a>
          <ul>
            <li><a href="../index.html">Manual pages and Users Manual</a></li>
            <li><a href="../referencing.html">Citing PETSc</a></li>
            <li><a href="index.html">Tutorials</a></li>
              <ul>
                <li><span class="current">Hands On Exercises</span></li>
              </ul>
            </li>
            <li><a href="../installation.html">Installation</a> </li>
            <li><a href="../saws.html">SAWs</a></li>
            <li><a href="../changes/index.html">Changes</a></li>
            <li><a href="../bugreporting.html">Bug Reporting</a></li>
            <li><a href="../codemanagement.html">Code Management</a></li>
            <li><a href="../faq.html">FAQ</a></li>
            <li><a href="../license.html">License</a></li>
            <li><a href="../linearsolvertable.html">Linear Solver Table</a></li>
          </ul>
        </li>
        <li><a href="../../publications/index.html">Applications/Publications</a></li>
        <li><a href="../../miscellaneous/index.html">Miscellaneous</a></li>
        <li><a href="../../miscellaneous/external.html">External Software</a></li>
        <li><a href="../../developers/index.html">Developers Site</a></li>
      </ul>
    </div>

    <div id="main">


<h4>PETSc comes with a large number of example codes to illustrate usage. The three main sets are in:</h4>

<ul>
  <li> <a href="#example1">Linear Poisson equation on a 2D grid</a>
    <ul>
      <li> example of linear equation problem </li>
      <li> see also <a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ksp/ksp/examples/tutorials/index.html">src/ksp/ksp/examples/tutorials</a> </li>
    </ul>
  </li>

  <li> <a href="#example2">Nonlinear ODE arising from a time-dependent one dimensional PDE</a>
    <ul>
      <li> example of time-stepping problem </li>
      <li> see also <a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ts/examples/tutorials/index.html">src/ts/examples/tutorials</a></li>
    </ul>
  </li>
  
  <li> <a href="#example3">Nonlinear PDE on a structured grid</a>
    <ul>
      <li> example of nonlinear PDE </li>
      <li> see also <a href="http://www.mcs.anl.gov/petsc/petsc-current/src/snes/examples/tutorials/index.html">src/snes/examples/tutorials</a> </li>
    </ul>
  </li>
  <li> <a href="#example4">Linear Stokes-type PDE on a structured grid</a> </li>
  <li> <a href="#example5">Nonlinear time dependent PDE on Unstructured Grid</a> </li>
</ul>

<p>
  Several examples are also included that represent the interoperability with
  other numerical software packages in the
  <a href="http://www.xsdk.info">xSDK Toolkit</a>. These packages can be
  automatically installed by PETSc by configuring with --download-trilinos, --download-hypre, and/or --download-superlu_dist.
  <ul>
    <li><a href="#ML">Nonlinear PDE Example</a> using <a href="http://trilinos.org/packages/ml">Trilinos preconditioner ML</a>
    <li><a href="#HYPRE">Nonlinear PDE Example</a> using BoomerAMG from <a href="http://computation.llnl.gov/project/linear_solvers/index.php">HYPRE</a>
    <li><a href="#SuperLU">Linear Equation Example</a> using direct solver <a href="http://crd-legacy.lbl.gov/~xiaoye/SuperLU/">SuperLU_DIST</a>
  </ul>
</p>
<!--------------------------------------------------------------------------->
<div id="example2"
<h2>Example 1: Linear Poisson equation on a 2D grid</h2>

<p>WHAT THIS EXAMPLE DEMONSTRATES:</p>
<ul>
  <li>Using command line options</li>
  <li>Using Linear Solvers</li>
  <li>Handling a simple structured grid</li>
</ul>

<p>FURTHER DETAILS:</p>
<ul>
  <li><a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ksp/ksp/examples/tutorials/ex50.c.html#line1">Mathematical description of the problem</a></li>
  <li><a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ksp/ksp/examples/tutorials/ex50.c.html#line21">the source code</a></li>
</ul>

<p>DO THE FOLLOWING:</p>
<ul>
  <li> Compile src/ksp/ksp/examples/tutorials/ex50.c
    <pre>
      cd petsc/src/ksp/ksp/examples/tutorials
      make ex50
  </pre>
  </li>
  <li> Run a 1 processor example with a 3x3 mesh and view the matrix assembled
    <pre>
     mpiexec -n 1 ./ex50  -da_grid_x 4 -da_grid_y 4 -mat_view     <a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ksp/ksp/examples/tutorials/output/ex50_tut_1.out.html">[Expected output]</a>
    </pre>
  </li>
  <div id="SuperLU">
  <li> Run with a 120x120 mesh on 16 processors using superlu_dist and view the solver options used
    <pre>
     mpiexec -n 16 ./ex50  -da_grid_x 120 -da_grid_y 120 -pc_type lu -pc_factor_mat_solver_type superlu_dist -ksp_monitor -ksp_view     <a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ksp/ksp/examples/tutorials/output/ex50_tut_2.out.html">[Expected output]</a>
    </pre>
  </div>
  </li>
  <li> Run with a 2049x2049 grid using multigrid solver on 16 processors with 10 multigrid levels
    <pre>
     mpiexec -n 16 ./ex50 -da_grid_x 2049 -da_grid_y 2049 -pc_type mg -pc_mg_levels 10 -ksp_monitor     <a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ksp/ksp/examples/tutorials/output/ex50_tut_3.out.html">[Expected output]</a>
    </pre>
  </l>
</ul>
</div>

<!--------------------------------------------------------------------------->
<div id="example2">
<h2>Example 2: Nonlinear ODE arising from a time-dependent one dimensional PDE</h2>

<p>WHAT THIS EXAMPLE DEMONSTRATES:</p>
<ul>
  <li>Using command line options</li>
  <li>Handling a simple structured grid</li>
  <li>Using the ODE integrator</li>
  <li>Using call-back functions</li>
</ul>

<p>FURTHER DETAILS:</p>
<ul>
  <li><a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ts/examples/tutorials/ex2.c.html#line13">Mathematical description of the problem</a></li>
  <li><a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ts/examples/tutorials/ex2.c.html#line36">the source code</a></li>
</ul>

<p>DO THE FOLLOWING:</p>
<ul>
  <li> Compile src/ts/examples/tutorials/ex2.c
    <pre>
      cd petsc/src/ts/examples/tutorials
      make ex2
  </pre>
  </li>
  <li> Run a 1 processor example on the default grid with all the default solver options
    <pre>
     mpiexec -n 1 ./ex2 -ts_max_steps 10 -ts_monitor     <a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ts/examples/tutorials/output/ex2_tut_1.out.html">[Expected output]</a>
    </pre>
  </li>
  <li> Run with the same options on 4 processors plus monitor convergence of the nonlinear and linear solvers
    <pre>
     mpiexec -n 4 ./ex2 -ts_max_steps 10 -ts_monitor -snes_monitor -ksp_monitor     <a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ts/examples/tutorials/output/ex2_tut_2.out.html">[Expected output]</a>
    </pre>
  </li>
  <li> Run with the same options on 16 processors with 128 grid points
    <pre>
     mpiexec -n 16 ./ex2 -ts_max_steps 10 -ts_monitor -M 128     <a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ts/examples/tutorials/output/ex2_tut_3.out.html">[Expected output]</a>
    </pre>
  </li>
</ul>
</div>

<!--------------------------------------------------------------------------->
<div id="example3">
<h2>Example 3: Nonlinear PDE on a structured grid</h2>

<p>WHAT THIS EXAMPLE DEMONSTRATES:</p>
<ul>
  <li>Handling a 2d structured grid</li>
  <li>Using the nonlinear solvers</li>
  <li>Changing the default linear solver</li>
</ul>

<p>FURTHER DETAILS:</p>
<ul>
  <li><a href="http://www.mcs.anl.gov/petsc/petsc-current/src/snes/examples/tutorials/ex19.c.html#line19">Mathematical description of the problem</a></li>
  <li><a href="http://www.mcs.anl.gov/petsc/petsc-current/src/snes/examples/tutorials/ex19.c.html#line94">main program source code</a></li>
  <li><a href="http://www.mcs.anl.gov/petsc/petsc-current/src/snes/examples/tutorials/ex19.c.html#line246">physics source code</a></li>
</ul>

<p>DO THE FOLLOWING:</p>
<ul>
  <li> Compile src/snes/examples/tutorials/ex19.c
    <pre>
      cd petsc/src/snes/examples/tutorials/
      make ex19
  </pre>
  </li>
  <li> Run a 16 processor example with six levels of grid refinement, monitor the convergence of the nonlinear and linear solver
      and examine the exact solver used
    <pre>
     mpiexec -n 16 ./ex19 -da_refine 6 -snes_monitor -ksp_monitor -snes_view     <a href="http://www.mcs.anl.gov/petsc/petsc-current/src/snes/examples/tutorials/output/ex19_tut_1.out.html">[Expected output]</a>
    </pre>
  </li>
  <li> Run with the same options but use geometric multigrid as the linear solver
    <pre>
     mpiexec -n 16 ./ex19 -da_refine 6 -snes_monitor -ksp_monitor -snes_view -pc_type mg     <a href="http://www.mcs.anl.gov/petsc/petsc-current/src/snes/examples/tutorials/output/ex19_tut_2.out.html">[Expected output]</a>
    </pre>
    Note this requires many fewer iterations than the default solver
  </li>
  <div id="HYPRE">
  <li> Run with the same options but use algebraic multigrid (hypre's BoomerAMG) as the linear solver
    <pre>
     mpiexec -n 16 ./ex19 -da_refine 6 -snes_monitor -ksp_monitor -snes_view -pc_type hypre     <a href="http://www.mcs.anl.gov/petsc/petsc-current/src/snes/examples/tutorials/output/ex19_tut_3.out.html">[Expected output]</a>
    </pre>
    Note this requires many fewer iterations than the default solver but requires more iterations than geometric multigrid
  </div>
  </li>
  <div id="ML">
  <li> Run with the same options but use the ML preconditioner from Trilinos (requires Trilinos)
    <pre>
     mpiexec -n 16 ./ex19 -da_refine 6 -snes_monitor -ksp_monitor -snes_view -pc_type ml     <a href="http://www.mcs.anl.gov/petsc/petsc-current/src/snes/examples/tutorials/output/ex19_tut_8.out.html">[Expected output]</a>
    </pre>
  </div>
  <li> Run on 4 processors with the default linear solver and profile the run
    <pre>
     mpiexec -n 4 ./ex19 -da_refine 6 -log_view     <a href="http://www.mcs.anl.gov/petsc/petsc-current/src/snes/examples/tutorials/output/ex19_tut_4.out.html">[Expected output]</a>
    </pre>
    Search for the line beginning with SNESSolve, the fourth column gives the time for the nonlinear solve.
  </li>
  <li> Run on 4 processors with the geometric multigrid linear solver and profile the run
    <pre>
     mpiexec -n 4 ./ex19 -da_refine 6 -log_view -pc_type mg     <a href="http://www.mcs.anl.gov/petsc/petsc-current/src/snes/examples/tutorials/output/ex19_tut_5.out.html">[Expected output]</a>
    </pre>
    Compare the runtime for SNESSolve to the case with the default solver
  </li>
  <li> Run on 16 processors with the default linear solver and profile the run
    <pre>
     mpiexec -n 16 ./ex19 -da_refine 6 -log_view     <a href="http://www.mcs.anl.gov/petsc/petsc-current/src/snes/examples/tutorials/output/ex19_tut_6.out.html">[Expected output]</a>
    </pre>
    Compare the runtime for SNESSolve to the 4 processor case with the default solver, what is the speedup?
  </li>
  <li> Run on 16 processors with the geometric multigrid linear solver and profile the run
    <pre>
     mpiexec -n 16 ./ex19 -da_refine 6 -log_view -pc_type mg     <a href="http://www.mcs.anl.gov/petsc/petsc-current/src/snes/examples/tutorials/output/ex19_tut_7.out.html">[Expected output]</a>
    </pre>
    Compare the runtime for the SNESSolve to the 4 processor case with multigrid, what is the speedup? 
    Why is the speedup for multigrid lower than the speedup for the default solver? 
  </li>
</ul>
</div>

<!--------------------------------------------------------------------------->
<div id="example4">
<h2>Example 4: Linear Stokes-type PDE on a structured grid</h2>

<p>WHAT THIS EXAMPLE DEMONSTRATES:</p>
<ul>
  <li>Handling a 3d structured grid</li>
  <li>Controlling linear solver options</li>
  <li>Selecting composible preconditioners</li>
  <li>Solving a Stokes problem</li>
  <li>Adding your own problem specific visualization</li>
</ul>

<p>FURTHER DETAILS:</p>
<ul>
  <li><a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ksp/ksp/examples/tutorials/ex42.c.html">Mathematical description of the problem</a></li>
  <li><a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ksp/ksp/examples/tutorials/ex42.c.html#line2059">main program source code</a></li>
  <li><a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ksp/ksp/examples/tutorials/ex42.c.html#line819">physics source code</a></li>
</ul>

<p>DO THE FOLLOWING:</p>
<ul>
  <li> Compile src/ksp/ksp/examples/tutorials/ex42.c
    <pre>
      cd petsc/src/ksp/ksp/examples/tutorials
      make ex42
  </pre>
  </li>
  <li> Solve with the default solver
    <pre>
     mpiexec -n 4 ./ex42  -stokes_ksp_monitor   <a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ksp/ksp/examples/tutorials/output/ex42_tut_1.out.html">[Expected output]</a>
    </pre>
    Note the poor convergence for even a very small problem
  </li>
  <li> Solve with a solver appropriate for Stoke's problems -stokes_pc_type fieldsplit -stokes_pc_fieldsplit_type schur
    <pre>
     mpiexec -n 4 ./ex42  -stokes_ksp_monitor -stokes_pc_type fieldsplit -stokes_pc_fieldsplit_type schur  <a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ksp/ksp/examples/tutorials/output/ex42_tut_2.out.html">[Expected output]</a>
    </pre>
  </li>
  <li> Solve with a finer mesh
    <pre>
     mpiexec -n 16 ./ex42  -mx 40 -stokes_ksp_monitor  -stokes_pc_type fieldsplit -stokes_pc_fieldsplit_type schur  <a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ksp/ksp/examples/tutorials/output/ex42_tut_3.out.html">[Expected output]</a>
    </pre>
  </li>
</ul>
</div>
<!--------------------------------------------------------------------------->
<div id="example5">
<h2>Example 5: Nonlinear time dependent PDE on Unstructured Grid</h2>

<p>WHAT THIS EXAMPLE DEMONSTRATES:</p>
<ul>
  <li>Changing the default ODE integrator</li>
  <li>Handling unstructured grids</li>
  <li>Registering your own interchangeable physics and algorithm modules</li>
</ul>

<p>FURTHER DETAILS:</p>
<ul>
  <li><a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ts/examples/tutorials/ex11.c.html">Mathematical description of the problem</a></li>
  <li><a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ts/examples/tutorials/ex11.c.html#line1403">main program source code</a></li>
  <li><a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ts/examples/tutorials/ex11.c.html#line186">source code of physics modules</a></li>
</ul>

<p>DO THE FOLLOWING:</p>
<ul>
  <li> Compile src/ts/examples/tutorials/ex11.c
    <pre>
      cd petsc/src/ts/examples/tutorials
      make ex11
  </pre>
  <li>Run simple advection through a tiny hybrid mesh
    <pre>
     mpiexec -n 1 ./ex11 -f ${PETSC_DIR}/share/petsc/datafiles/meshes/sevenside.exo      <a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ts/examples/tutorials/output/ex11_tut_1.out.html">[Expected output]</a>
    </pre>
  </li>
  <li>Run simple advection through a small mesh with a Rosenbrock-W solver
    <pre>
     mpiexec -n 1 ./ex11 -f ${PETSC_DIR}/share/petsc/datafiles/meshes/sevenside.exo -ts_type rosw      <a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ts/examples/tutorials/output/ex11_tut_2.out.html">[Expected output]</a>
    </pre>
  </li>
  <li>Run simple advection through a larger quadrilateral mesh of an annulus with least squares reconstruction and no limiting, monitoring the error
    <pre>
     mpiexec -n 16 ./ex11 -f ${PETSC_DIR}/share/petsc/datafiles/meshes/annulus-20.exo -monitor Error -advect_sol_type bump -petscfv_type leastsquares -petsclimiter_type sin <a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ts/examples/tutorials/output/ex11_tut_4.out.html">[Expected output]</a>
    </pre>
    Compare turning to the error after turning off reconstruction.
  </li>
  <li>Run shallow water on the larger mesh with least squares reconstruction and minmod limiting, monitoring water Height (integral is conserved) and Energy (not conserved)
    <pre>
     mpiexec -n 4 ./ex11 -f ${PETSC_DIR}/share/petsc/datafiles/meshes/annulus-20.exo -physics sw -monitor Height,Energy -petscfv_type leastsquares -petsclimiter_type minmod <a href="http://www.mcs.anl.gov/petsc/petsc-current/src/ts/examples/tutorials/output/ex11_tut_5.out.html">[Expected output]</a>
    </pre>
  </li>
</ul>
</div>
</body></html>
